<?php
/**
 * 《快递需求》实现类
 *
 * @author 孙晓晔
 * @version $Id: Apply.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/ExpressUtil.class.php');

class Requirement extends ExpressUtil {
	public $tbl = Constant::tbl_express_requirement;
	
	var $info_dir = 'express';
	
	public $type = array(
		// '-' => '请选择',
		'1' => '产品',
		'2' => '样品',
		'3' => '产品',	
		
		'4' => '货物',
		'5' => '文件',
	
		'9' => '其它',
	);
	
	public $payment = array(
		'-' => '请选择',
		'1' => '自付',	
		'2' => '到付',
			
		'９' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待走货',
		'3' => '正在走货',
		'4' => '已经走货',

		'5' => '暂停走货',
		'6' => '取消走货',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '申请人',
		'type' => '类型',
		'way' => '方式',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'emp' => '申请人',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('type_option', $this->type);
		$controller->assign_by_ref('payment_option', $this->payment);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 year'));
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '负责人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'express/requirement/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$name_s = $request['name_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		} 
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}
		
		$where .= $this->_where_x("way", $request['way_s']);
		$where .= $this->_where_x("type", $request['type_s']);
		
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}
		
		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		if ($this->_audit('140')) {
			return '';
		}

		$emp = $_SESSION['id'];
		
		return sprintf(" (sales = '%s' OR engineer = '%s' OR emp = '%s')", $emp, $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'express/requirement/list');
		return true;
	}

	function _plan2($id) {
		$row = Common::id(Constant::tbl_express_plan, $id);
		if (is_array($row)) {
			return sprintf("%s/%s", Name::supplier($row['supplier']), $row['code']);
		}

		return '';
	}
	
	function _plan($id) {
		require_once(dirname(__FILE__) . '/Plan.class.php');
		$aa = new Plan();
		$row = $aa->_view($id);
		
		if (empty($row['supplier'])) {
			return '';
		}
		
		$row['s'] = sprintf("%s/%s", Name::supplier($row['supplier']), $row['id']);
		
		return $row;
	}
	
	function _plan_requirement($id) {
		$db = newdb($this->dsn_r);
		// $fmt = "SELECT * FROM %s WHERE status = '3' AND plan = '%s' ORDER BY id";
		$fmt = "SELECT * FROM %s WHERE plan = '%s' ORDER BY id";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['mold_t'] = Name::mold($row['mold']);
			$row['type_t'] = $this->type[$row['type']];
			$row['way_t'] = $this->way[$row['way']];
				
			$row['emp_t'] = Name::emp($row['emp']);

			$ret[] = $row;
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('1 day'));

		$controller->display($request, 'express/requirement/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$mold = $this->_input($request['mold']);
		
		$name = addslashes($request['name']);
		$type = $request['type'];
		$way = $request['way'];
		$payment = $request['payment'];
		
		$address = addslashes($request['address']);
		$recipient = addslashes($request['recipient']);
		$mobile = addslashes($request['mobile']);
		
		$quantity = addslashes($request['quantity']);		
		$measure = addslashes($request['measure']);		
		$weight1 = addslashes($request['weight1']);
		$weight2 = addslashes($request['weight2']);
		
		$content = addslashes($request['content']);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, mold, name, type, way, payment, address, recipient, mobile, measure, quantity, weight1, weight2, content, oper, createtime, modifytime) ";
		$fmt .= "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $mold, $name, $type, $way, $payment, $address, $recipient, $mobile, $measure, $quantity, $weight1, $weight2, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('file', $this->_file($row));

		$controller->display($request, 'express/requirement/upload');
		return true;
	}
	
	function uploadPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '\n%s'), modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function ajax_upload($controller, $request) {
		require_once 'HTTP/Upload.php';
		$upload = new HTTP_Upload('en');
		$file = $upload->getFiles('Filedata');
		if (PEAR::isError($file)) {
			die($file->getMessage());
		}

		if ($file->isValid()) {
			$now = date('Y-m-d H:i:s');

			$id = $request['id'];
			$name = $request['name'];
			if (empty($name) || $name == '-') {
				$name = 'uniq';
			}

			$dir = Download::dir($this->info_dir, $now, $id);
			// debug($dir);
			if (!@file_exists($dir)) {
				umask();
				mkdir($dir, 0777, true);
			}

			$file->setName($name);
			$filename = $file->moveTo($dir);
			if (PEAR::isError($filename)){
				die($filename->getMessage());
			}

			$filesize = $file->upload['size'];

			$db = newdb($this->dsn_w);
			$fmt = "INSERT INTO %s(info, name, size, ip, createtime) VALUES('%s', '%s', '%s', '%s', now())";
			$sql = sprintf($fmt, Constant::tbl_express_file, $id, $filename, $filesize, $_SERVER['REMOTE_ADDR']);
			// debug($sql);
			$db->query($sql);
		}

		echo "1";
		return true;
	}
	
	function _file($data) {
		$db = newdb(Constant::tbl_express_file);
		$fmt = "SELECT * FROM %s WHERE info = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, Constant::tbl_express_file, $data['id']);
		// debug($sql);
		$result = $db->query($sql);

		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['file'] = Download::url($this->info_dir, $data['createtime'], $data['id'], $row['name']);
			$row['size_t'] = printsize($row['size']);
			$row['ok'] = true;

			$ret[] = $row;
		}

		return $ret;
	}

	function _notice_1($controller, $request, $id) {
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$subject = sprintf("货运需求 %s %s", $row['id'], $row['emp_t']);
		$body = $controller->sunxy($request, 'express/requirement/notice_1');
		debug($body);

		$this->_notice_x($row['emp'], '112', $subject, $body);
	}
	
	function _notice_x($emp, $service, $subject, $body) {
		$bb = $this->_list_service($service);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		while (list($k, $to) = each($bb)) {
			// $to = '101';
			// echo $to . "\n";
			$this->_notice($emp, $to, $subject, $body, 1);
		}
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		// $row['log'] = json_encode($this->_file($row));
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'express/requirement/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];

		$mold = $this->_input($request['mold']);
		
		$name = addslashes($request['name']);		
		$type = $request['type'];
		$way = $request['way'];
		$payment = $request['payment'];				
		
		$address = addslashes($request['address']);
		$recipient = addslashes($request['recipient']);
		$mobile = addslashes($request['mobile']);
		
		$quantity = addslashes($request['quantity']);		
		$measure = addslashes($request['measure']);		
		$weight1 = addslashes($request['weight1']);
		$weight2 = addslashes($request['weight2']);
		
		$content = addslashes($request['content']);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', mold = '%s', name = '%s', type = '%s', way = '%s', payment = '%s', address = '%s', recipient = '%s', mobile = '%s', ";
		$fmt .= "measure = '%s', quantity = '%s', weight1 = '%s', weight2 = '%s', content = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $mold, $name, $type, $way, $payment, $address, $recipient, $mobile, $measure, $quantity, $weight1, $weight2, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		// $this->_update_file($id, $request['log']);	

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_file($id, $log) {
		$db = newdb($this->dsn_w);		
		while (list($k, $v) = each($log)) {	
			if (array_key_exists("createtime", $v)) {				
				if (!$this->_ok($v['ok'])) {
					$fmt = "DELETE FROM %s WHERE id = '%s'";
					$sql = sprintf($fmt, Constant::tbl_express_file, $v['id']);
					debug($sql);
					$db->query($sql);
				}			
			}			
		}
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'express/requirement/copy');
		return true;
	}

	/**
	 * 审核
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verify($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'express/requirement/verify');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		// $this->_notice_9($id, $status);
		$this->_notice_1($controller, $request, $id);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _notice_9($id, $status) {
		$row = $this->id($this->tbl, $id);

		if ($status == '2') {
			$subject = "[通知]用车申请审核通过，等待派车";
		} else {
			$subject = "[通知]用车申请审核未通过";
		}

		$body = sprintf("事由：%s\n路线：%s\n时间：%s\n\n%s\n%s\n", $row['purpose'], $row['address'], $this->_date($row['t1'], $row['t2']), Name::emp($_SESSION['id']), date('Y-m-d H:i:s'));

		$this->_notice($_SESSION['id'], $row['emp'], $subject, $body);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('file', $this->_file($row));
		
		if (!empty($request['print'])) {
			$controller->display($request, 'express/requirement/view_print');
		} else {
			$controller->display($request, 'express/requirement/view');
		}

		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
						
		$row['type_t'] = $this->type[$row['type']];
		$row['way_t'] = $this->way[$row['way']];
		$row['payment_t'] = $this->payment[$row['payment']];
		
		$row['mold_t'] = Name::mold($row['mold']);
		
		$row['p'] = $this->_plan($row['plan']);
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$row = $this->id($this->tbl, $id);
			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);				
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'express/requirement/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(t1, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(t1, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(t1, '%Y-%m')";
		}

		return $px;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}	

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function plan($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		require_once(dirname(__FILE__) . '/Plan.class.php');
		$plan = new Plan();
		$data = $plan->_list();
		
		$controller->assign_by_ref('plan_option', $data);

		$controller->display($request, 'express/requirement/plan');
		return true;
	}

	/**
	 * 货运安排
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function planPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$plan = $this->_input($request['plan']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET plan = '%s', content = concat(content, '%s'), status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $plan, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		// $this->_notice_2($id, $plan);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _notice_2($id, $plan_id) {
		$requirement = $this->id($this->tbl, $id);

		require_once(dirname(__FILE__) . '/Plan.class.php');
		$plan = new Plan();
		$row = $plan->_view($plan_id);

		$subject = "[通知]已经派车";
		$body = sprintf("车辆：%s\n司机：%s\n路线：%s\n时间：%s\n\n%s\n%s\n", $row['car_t'], $row['driver_t'], $row['address'], $row['date'], Name::emp($_SESSION['id']), $row['createtime']);

		$this->_notice($_SESSION['id'], $requirement['emp'], $subject, $body);
	}

	/**
	 * 直接货运
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function freight($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'express/requirement/freight');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reason($controller, $request) {
		$this->pretreat($controller);

		$status = $request['status'];
		$controller->assign_by_ref('reason', $this->status[$status]);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'express/requirement/reason');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reasonPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$reason = addslashes($request['reason']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '\n%s'), status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status_continue($controller, $request) {
		$this->pretreat($controller);

		return $this->status_restart($controller, $request);
	}

	function status_restart($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$row = $this->id($this->tbl, $id);
		if (empty($row['plan'])) {
			$status = 2;
		} else {
			$status = 3;
		}

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email('304');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '货运申请';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}
		
		$controller->clear_assign('result');

		$body = $this->_tj_html($controller, $request, 'express/requirement/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {
		$bb = $this->_list_email('304');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->pretreat($controller);

		$from = $this->email;
		$subject = '货运申请（周报）';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);

		$body = $this->_tj_html($controller, $request, 'express/requirement/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
		
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'emp';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function t($controller, $request) {
		// $s = "TM22.33-2";
		$s = "TM22.32-3";
		
		$m = $this->_cp($s);
		print_r($m);

		logdate($controller, $request);
		return true;
	}
	
	function _cp($s) {
		if (preg_match("/(.+)\.(.+)-(\d+)$/i", $s, $m)) {
			return $m;
		}
		
		if (preg_match("/(.+)\.(\d+)/i", $s, $m)) {
			return $m;
		}
		
		return array();
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE ns_x is NULL";
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$row['mold_t'] = Name::mold($row['mold']);
			
			$m = $this->_cp($row['mold_t']);
			$code = $m[1];
			$project = $m[1] . "." . $m[2];

			$fmt = "UPDATE %s SET code = '%s', project = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $code, $project, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function view_mold($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_mold($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'express/requirement/_list_c');
		return true;
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'express/requirement/_list_c');
		return true;
	}
	
	function move($from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		debug($sql);
		$db->query($sql);
	}
}
?>
